package com.tbs.parkingsaleapp.UI;

import abstergo.tbs.betterasyc.Handler.HandlerMananger;
import abstergo.tbs.betterasyc.Handler.IHandlerAction;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.PermissionRequester;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.Listenners.LoginNeed;
import com.tbs.parkingsaleapp.databinding.ActivityHomeBinding;

import static com.tbs.parkingsaleapp.SystemManager.Handler.HandlerConst.LOGINED;

public class HomeActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityHomeBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PermissionRequester.verifyStoragePermissions(HomeActivity.this,PermissionRequester.permissions);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarHome.toolbar);
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
         View view=navigationView.getHeaderView(0);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);


        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_order,R.id.nav_list,R.id.nav_aboutme)
                .setOpenableLayout(drawer)
                .build();

        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        //处理导航栏标题
        TextView fview=view.findViewById(R.id.home_user);
        HandlerMananger.Subscribe(LOGINED, new IHandlerAction() {
            @Override
            public void Handle(Message rawmsg) {
                String text="处理中......";
                if(SystemDataMananger.isLogined())
                {
                    text= SystemDataMananger.getLogined().getUserinfo().getUserName();
                }
                else
                {
                    text="匿名游客";
                }
                fview.setText(text);
            }
        },true);
        ImageView imgv=view.findViewById(R.id.userImg);
        imgv.setOnClickListener(new LoginNeed(this));
        SystemDataMananger.Init(this);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}